package com.company.woye.onpackage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.company.woye.R;
import com.company.woye.console.adapter.LoadTXMAdapter;
import com.company.woye.onpackage.entity.OnPackageListEntity;
import com.company.woye.onpackage.entity.OnSonPackageListEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OnPackageListViewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<OnPackageListEntity> listEntities;
    private OnSonPackageListViewAdapter adapter;
    private OnPackageListViewAdapter.OnItemClickListener onItemClickListener;

    public OnPackageListViewAdapter(Context context, List<OnPackageListEntity> listEntities, OnPackageListViewAdapter.OnItemClickListener onItemClickListener) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.listEntities = listEntities;
        this.onItemClickListener=onItemClickListener;
    }

    @Override
    public int getCount() {
        return listEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return listEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHodler = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_adapter_on_package, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }
        viewHodler.tvBoxNum.setText(listEntities.get(position).getBoxNum());
        viewHodler.tvCarState.setText(listEntities.get(position).getCarState());
        viewHodler.tvCjName.setText(listEntities.get(position).getCj_name());
        viewHodler.tvLsx.setText(listEntities.get(position).getLsx());
        viewHodler.tvNeedTaskState.setText(listEntities.get(position).getNeedTaskState());
        viewHodler.tvSerialNo.setText(listEntities.get(position).getSerialNo());
        viewHodler.tvStyleNo.setText(listEntities.get(position).getStyleNo());
        viewHodler.tvZydCode.setText(listEntities.get(position).getZydCode());
        if (listEntities.get(position).getSonPackageListEntityList()!=null&&listEntities.get(position).getSonPackageListEntityList().size()>0){
            if (position<listEntities.size()) {
                adapter = new OnSonPackageListViewAdapter(mContext, listEntities.get(position).getSonPackageListEntityList());
                viewHodler.lv.setAdapter(adapter);
            }
        }
        setListViewHeightBasedOnChildren(viewHodler.lv);
        viewHodler.ll_on_package.setOnClickListener(v -> onItemClickListener.onItemClick(position));
        viewHodler.lv.setOnItemClickListener((parent1, view, i, id) -> onItemClickListener.onItemClick(position));
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.tv_cj_name)
        TextView tvCjName;
        @BindView(R.id.tv_lsx)
        TextView tvLsx;
        @BindView(R.id.tv_CarState)
        TextView tvCarState;
        @BindView(R.id.tv_ZydCode)
        TextView tvZydCode;
        @BindView(R.id.tv_SerialNo)
        TextView tvSerialNo;
        @BindView(R.id.tv_StyleNo)
        TextView tvStyleNo;
        @BindView(R.id.tv_BoxNum)
        TextView tvBoxNum;
        @BindView(R.id.tv_NeedTaskState)
        TextView tvNeedTaskState;
        @BindView(R.id.lv)
        ListView lv;
        @BindView(R.id.ll_on_package)
        LinearLayout ll_on_package;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //防止嵌套不显示
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    public  interface OnItemClickListener{
        void onItemClick(int position);
    }

}
